<?php

namespace utils;

class Utils
{
//获取器 提前修改
    public function getNicknameAttr($v)
    {
        if (self::is_utf8(base64_decode($v)) && self::str_is_base64($v) != '') {
            $data = base64_decode($v);
            if ($data == '')
                return $v;
            return $data;
        }
        return $v;
    }

    public static function is_utf8($str)
    {
        $len = strlen($str);
        for ($i = 0; $i < $len; $i++) {
            $c = ord($str[$i]);
            if ($c > 128) {
                if (($c > 247)) {
                    return false;
                } elseif ($c > 239) {
                    $bytes = 4;
                } elseif ($c > 223) {
                    $bytes = 3;
                } elseif ($c > 191) {
                    $bytes = 2;
                } else {
                    return false;
                }
                if (($i + $bytes) > $len) {
                    return false;
                }
                while ($bytes > 1) {
                    $i++;
                    $b = ord($str[$i]);
                    if ($b < 128 || $b > 191) {
                        return false;
                    }
                    $bytes--;
                }
            }
        }
        return true;
    }

    /**
     * 判断字符串是不是base64编码
     */
    public function str_is_base64($str)
    {
        return $str == base64_encode(base64_decode($str)) ? true : false;
    }

    public static function array_addOrUpValue($array,$key,$value)
    {
        if (array_key_exists($key, $array)) {

            //unset($array[$key]);
            $array[$key] = $value;
        }else{
            $array[$key] = $value;
        }
    }

    public static function getValueByArray($array,$key)
    {
        if (array_key_exists($key, $array)) {

            //unset($array[$key]);
            return $array[$key];
        }else{
            return false;
        }
    }
}